#include "Player.h"

Player::Player() {
	sox = NULL;
	numActions = 2;
}

Player::Player(int _numActions) {
	numActions = _numActions;
}

Player::~Player() {
	if (sox != NULL)
		delete sox;
}

void Player::setUpConnection(int port) {
	//matlab = false;

	char buf[1024];

	sox = new MySocket(port);
	sox->AcceptEm();
	sox->ReadMessage(buf);
	//printf("%s\n", buf);
	if (!strncmp(buf, "row", 3))
		type = ROW;
	else
		type = COL;
	if (strlen(buf) > 5) {
		strcpy(nombre, strtok(buf+5, "\r\n"));
	}
	else {
		sox->ReadMessage(buf);
		strcpy(nombre, strtok(buf, "\r\n"));
	}
	printf("%s is connected\n", nombre);
}

void Player::getAction() {
	char buf[1024];

	//if (matlab) {
	//	intendedAction = sox->readmsg();
	//}
	//else {
		sox->ReadMessage(buf);
		intendedAction = atoi(buf);
	//}
}

void Player::move(double errorRate) {
	//double num = (double)rand() / RAND_MAX;
	//if (num >= errorRate)
		action = intendedAction;
	//else {
	//	action = rand() % numActions;
	//	while (action == intendedAction)
	//		action = rand() % numActions;
	//}
}

void Player::setUpMatlabConnection(int port) {
	//matlab = true;

	sox = new MySocket(port);
	sox->AcceptEm();
	int tipo = sox->readmsg();
	if (tipo == 0)
		type = ROW;
	else
		type = COL;
	//strcpy(nombre, "matlab");
	char buf[1024];
	sox->readmsg(nombre);
	printf("%s is connected\n", nombre);
}